import _config from '../../common/config'

/**
 * 请求拦截
 * @param {Object} http
 */
module.exports = vm => {
  uni.$u.http.interceptors.request.use(
    config => {
      // 拦截请求
      config.data = config.data || {}
      // if (config.method == "GET") {
      //   config.params = config.data || {};
      // } else {
      //   config.data = config.data || {};
      // }

      let header_ = {}
      if (_config.host) header_['redirectHost'] = _config.host // 请求转发

      const _token = vm.$store.getters.token
      if (_token) header_['Authorization'] = `Bearer ${_token}` // 携带token

      config.header = header_
      console.log('发送请求：', config)
      uni.showLoading({ title: '请稍后' })
      return config
    },
    config => Promise.reject(config)
  )
}
